const express = require('express');
const router = express.Router();
const axios = require('axios');


/* GET home page. */
/* 根据城市编码获取天气 */
router.post('/', async (req, res, next) => {
    try {
        const {location,identification} = req.body
        //校验参数是否合法
        if (!location){
            res.status(400).json({
                code: 400,
                msg: '查询天气缺少必要参数',
                data:''
            })
            return
        }

        //根据城市码，获取天气
        const result = await axios.get('https://api.qweather.com/v7/weather/now',{
            params:{
                key:'60322c744b694fec8eb26a706fd47a77',
                location:location,
            }
        })
        //返回的数据
        res.json({
            code: 200,
            msg: '天气获取成功',
            data:{
                weather:result.data.now.text
            }

        });
    }
    catch (e) {
        res.status(401).json({
            msg: 401,
            message: '访问异常' + e.message
        });
    }

})

module.exports = router;